﻿using B.S.Material.Domain.Production_rejection;
using B.S.Material.ErrorCode;
using B.S.Material.Read.Api.Applications.Command.StrippingCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Read.Api.Applications.Handler.StrippingHandler
{
    public class GetStrippingHandler : IRequestHandler<GetStrippingCommand, ApiResult<ApiPaging<Stripping>>>
    {
        private readonly ISqlSugarClient db;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="config"></param>
        public GetStrippingHandler(ISqlSugarClient db)
        {
            this.db = db;

        }
        public async Task<ApiResult<ApiPaging<Stripping>>> Handle(GetStrippingCommand request, CancellationToken cancellationToken)
        {


            ApiResult<ApiPaging<Stripping>> result = new ApiResult<ApiPaging<Stripping>>();

            var query = await db.Queryable<Stripping>().Where(x => !x.IsDel).ToListAsync();

            //条件查询

            if (!string.IsNullOrEmpty(request.subject))
            {
                query = query.Where(x => x.subject.Contains(request.subject)).ToList();
            }
            if (!string.IsNullOrEmpty(request.documentNumber))
            {
                query = query.Where(x => x.documentNumber.Contains(request.documentNumber)).ToList();
            }
            if (request.returndate != null)
            {
                query = query.Where(x => x.returndate.Date == request.returndate.Value.Date).ToList();
            }
            if (!string.IsNullOrEmpty(request.operatorName))
            {
                query = query.Where(x => x.operatorName.Contains(request.operatorName)).ToList();
            }
            int totalCount = query.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            query = query.OrderByDescending(a => a.StrippingId).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            ApiPaging<Stripping> paging = new ApiPaging<Stripping>()
            {
                totalCount = totalCount,
                PageCount = PageCount,
                Result = query.ToList(),
            };

            result.Code = ApiEnum.显示成功;
            result.MAG = ApiEnum.显示成功.ToString();
            result.data = paging;
            return result;

            // 进行两表联查
            //var query = db.Queryable<Stripping, StrippingDetails>((dp, dd) => dp.StrippingId == dd.StrippingId)
            //              .Select((dp, dd) => new StrippingDto
            //              {
            //                  subject = dp.subject,
            //                  documentNumber = dp.documentNumber,
            //                  returndate = dp.returndate,
            //                  operatorName = dp.operatorName,
            //                  remark = dp.remark,
            //                  returnquantity = dd.returnquantity,
            //                  availablequantity = dd.availablequantity,
            //                  IsDel = dp.IsDel,
            //              });

            ////条件查询

            //if (!string.IsNullOrEmpty(request.subject))
            //{
            //    query = query.Where(x => x.subject.Contains(request.subject));
            //}
            //if (!string.IsNullOrEmpty(request.documentNumber))
            //{
            //    query = query.Where(x => x.documentNumber.Contains(request.documentNumber));
            //}
            //if (request.returndate != null)
            //{
            //    query = query.Where(x => x.returndate.Date == request.returndate.Value.Date);
            //}
            //if (!string.IsNullOrEmpty(request.operatorName))
            //{
            //    query = query.Where(x => x.operatorName.Contains(request.operatorName));
            //}
            //int TotalCount = query.Count();
            //int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.pageSize);
            //query = query.OrderByDescending(dd => dd.StrippingId).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize);

            //ApiPaging<StrippingDto> paging = new ApiPaging<StrippingDto>();
            //paging.totalCount = TotalCount;
            //paging.PageCount = TotalPage;
            //paging.Result = query.ToList();

            //result.Code = ApiEnum.显示成功;
            //result.MAG = ApiEnum.显示成功.ToString();
            //result.data = paging;

            //return await Task.FromResult(result);




        }
    }
}
